package mini.ideashare.cms.base;

import java.io.Serializable;
import java.util.List;

/**
 * @author lixiang
 * @date 2018/04/02
 */
public class BaseResponse<T> implements Serializable {


    private static final long serialVersionUID = -6707681616003589614L;
    private boolean success = true;

    private String message;
    private String messageCode;
    /**
     *  data返回list时T需要继承 PageData
     *  data为单个vo时，T不需要继承 PageData
     */
    private T data;

    public BaseResponse() {
    }

    public BaseResponse(boolean success, T data) {
        this.success = success;
        this.data = data;
    }


   public BaseResponse(boolean success, String message) {
        this.success = success;
        this.message = message;
    }

    public BaseResponse(boolean success, String message, String messageCode) {
        this.success = success;
        this.message = message;
        this.messageCode = messageCode;
    }

    public BaseResponse(boolean success, String message, T data) {
        this.success = success;
        this.message = message;
        this.data = data;
    }

    public static  <T> BaseResponse<T> assembleResponse(T data, String message, boolean success) {
        BaseResponse<T> baseResponse = new BaseResponse<T>();
        baseResponse.setData(data);
        baseResponse.setMessage(message);
        baseResponse.setSuccess(success);
        return baseResponse;
    }
    public static  <T> BaseResponse<T> assembleResponse(T data, String message, String messageCode,boolean success) {
        BaseResponse<T> baseResponse = new BaseResponse<T>();
        baseResponse.setData(data);
        baseResponse.setMessage(message);
        baseResponse.setSuccess(success);
        baseResponse.setMessageCode(messageCode);
        return baseResponse;
    }


    public static  <T> BaseResponse<T> assembleResponse(boolean success, String messageCode,String message) {
        BaseResponse<T> baseResponse = new BaseResponse<T>();
        baseResponse.setMessage(message);
        baseResponse.setSuccess(success);
        baseResponse.setMessageCode(messageCode);
        return baseResponse;
    }




    public static  <T> BaseResponse<T> assembleResponse(T data, boolean success) {
        return assembleResponse(data, "", success);
    }

    public static  <T> BaseResponse<T> assembleResponse(T data) {
        return assembleResponse(data, true);
    }

    public static  <T> BaseResponse<PageData<T>> assemblePageResponse(List<T> data, Long totalCount, int pageIndex, int pageSize) {
        PageData<T> pageData = new PageData<>(pageIndex, pageSize, totalCount, data);
        return assembleResponse(pageData);
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessageCode() {
        return messageCode;
    }

    public BaseResponse<T> setMessageCode(String messageCode) {
        this.messageCode = messageCode;
        return this;
    }

}
